<?php
$clients = array();
$features = array();
if (file_exists('./clients.dat')) {
 $clients = unserialize(file_get_contents('./clients.dat'));
} 
if ($clients == null) {
$clients = array(
               1=>'DMDirc',
               2=>'mIRC',
               3=>'Konversation',
               4=>'X-Chat',
               5=>'Klient',
               );
}
if (file_exists('features.dat')) {
 $features = unserialize(file_get_contents('features.dat'));
}
if ($features == null) {
$features = array (
'OS' => array ( 
              'Windows' => array('DMDirc'=>1, 'mIRC'=>1, 'Konversation'=>0, 'X-Chat'=>1, 'Klient'=>1),
              'Linux' => array('DMDirc'=>1, 'mIRC'=>0, 'Konversation'=>1, 'X-Chat'=>1, 'Klient'=>2),
              'OS X' => array('DMDirc'=>1, 'mIRC'=>0, 'Konversation'=>0, 'X-Chat'=>2, 'Klient'=>2),
              ),
'License' => array (
                   'Open-Source' => array('DMDirc'=>1, 'mIRC'=>0, 'Konversation'=>1, 'X-Chat'=>1, 'Klient'=>2),
                   ),
'Server support' => array (
                          'Multi-Server' => array('DMDirc'=>1, 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>1, 'Klient'=>2),
                          'SSL Servers' => array('DMDirc'=>'Planned', 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>2, 'Klient'=>2),
                          ),
'GUI type' => array (
                    'MDI' => array('DMDirc'=>1, 'mIRC'=>1, 'Konversation'=>0, 'X-Chat'=>2, 'Klient'=>2),
                    ),
'Control code support' => array (
                                'Bold' => array('DMDirc'=>'0.2', 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>2, 'Klient'=>2),
                                'Underline' => array('DMDirc'=>'0.2', 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>2, 'Klient'=>2),
                                'Italics' => array('DMDirc'=>'0.2', 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>2, 'Klient'=>2),
                                'Colour' => array('DMDirc'=>'0.2', 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>2, 'Klient'=>2),
                                'Reverse' => array('DMDirc'=>'0.2', 'mIRC'=>1, 'Konversation'=>1, 'X-Chat'=>2, 'Klient'=>2),
                                ),
);
}
function sortFeatures() {
 global $features, $clients;
 ksort($features);
 foreach ($features as $featureGroup) {
  ksort($featureGroup);
 }
}
function delFeature($groupName, $featureName) {
 global $features, $clients;
 $groupName = urldecode($groupName);
 $featureName = urldecode($featureName);
 unset($features[$groupName][$featureName]);
}
function delFeatureGroup($groupName) {
 global $features, $clients;
 $groupName = urldecode($groupName);
 unset($features[$groupName]);
}
function addFeature($groupName, $featureName) {
 global $features, $clients;
 if (!isset($features)) addFeatureGroup($groupName);
 foreach($clients as $client) {
 $features[$groupName][$featureName][$client] = 2;
 }
}
function addFeatureGroup($groupName) {
 global $features, $clients;
 $features[$groupName] = array();
}
function clientSort ($a, $b) {
 if ($b == 'DMDirc') { return -1; }
 if ($a == 'DMDirc') { return  1; }
 return strcmp(strtolower($a),strtolower($b));
}
function sortClients() {
 global $features, $clients;
 uasort($clients, 'clientSort');
   foreach($features as $featureGroup => $featureGroupList) {
   foreach($featureGroupList as $featureName => $clientsSupportingFeature) {
    uksort($features[$featureGroup][$featureName], 'clientSort');
   }
  }
}
function addClient($client) {
 global $features, $clients;
 if (!array_search($client, $clients)) {
  $clients[] = $client;
  foreach($features as $featureGroup => $featureGroupList) {
   foreach($featureGroupList as $featureName => $clientsSupportingFeature) {
    $features[$featureGroup][$featureName][$client] = 2;
   }
  }
 }
}
function delClient($client) {
 global $features, $clients;
 if (array_search($client, $clients)) {
  $key = array_search($client, $clients);
  unset($clients[$key]);
  foreach($features as $featureGroup => $featureGroupList) {
   foreach($featureGroupList as $featureName => $clientsSupportingFeature) {
    unset($features[$featureGroup][$featureName][$client]);
   }
  }
 }
}
function getSupported($group, $feature, $client) {
 global $features, $clients;
 return $features[$group][$feature][$clients[$client]];
}
function setSupported($group, $feature, $client, $value) {
 global $features, $clients;
 $features[$group][$feature][$client] = $value;
}
function save() {
 global $features, $clients;
 file_put_contents('./clients.dat', serialize($clients));
 file_put_contents('./features.dat', serialize($features));
}
?>
